cmake_minimum_required(VERSION 3.10)
project(opus_jni LANGUAGES C)

set(JAVA_HOME $ENV{JAVA_HOME})
message("JAVA_HOME = ${JAVA_HOME}")
if(NOT JAVA_HOME)
    message(WARNING "JAVA_HOME is not set or is empty.")
else()
    message(STATUS "JAVA_HOME is correctly set to: ${JAVA_HOME}")
endif()

# 设置动态库名称
set(LIB_NAME opus_jni)

# 添加源文件
set(SOURCES opus_wrapper.c)

# 创建动态库
add_library(${LIB_NAME} SHARED ${SOURCES})

# 平台特定的配置
if(WIN32)
    # Windows 平台
    # 设置头文件目录
    include_directories(${CMAKE_SOURCE_DIR}/include)
    include_directories(${JAVA_HOME}/include)
    include_directories(${JAVA_HOME}/include/win32)

    # 链接 libopus.dll
    target_link_libraries(${LIB_NAME} ${CMAKE_SOURCE_DIR}/libopus.lib)
else()
    # Linux 平台
    # 使用 pkg-config 查找 opus 库
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(OPUS REQUIRED opus)
    # 设置头文件目录
    include_directories(${JAVA_HOME}/include)
    include_directories(${JAVA_HOME}/include/linux)
    include_directories(${OPUS_INCLUDE_DIRS})
    # 链接 opus 库
    target_link_libraries(${LIB_NAME} ${OPUS_LIBRARIES})
endif()
